<?php
namespace admin\controllers;

use admin\models\UserPhoto;
use Yii;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;

/**
 * Created by PhpStorm.
 * User: 沈小鱼
 * Date: 2016/7/26
 * Time: 19:37
 */
class PhotoController extends BaseController{

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['upload','list','del','add'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
        ];
    }



    public function actionList(){
        $user_id = Yii::$app->request->get('user_id');
        $photo = UserPhoto::findOne(['mu_id'=>$user_id]);
        if(empty($photo)||empty($photo->mp_other)){
            $photo = '';
        }
        return $this->renderPartial('list',[
            'photo'=>$photo,
        ]);
    }


    public function actionUpload(){
        $admin = Yii::$app->user->identity;
        if(empty($admin)){
            echo json_encode([
                'imageUrl'=>'',
                'error'=>'登录信息失效，请重新登录',
            ]);
            exit;
        }
        $id = Yii::$app->request->post('id');
        $userPhoto = new UserPhoto();
        if(Yii::$app->request->isPost) {
            $image = UploadedFile::getInstance($userPhoto, 'mp_other');
            $path = 'photo/users/'.$id;
            if(!is_dir($path) || !is_writable($path)){
                FileHelper::createDirectory($path,0777,true);
            }
            $filePath = $path  . '/' . $id . '_' .md5(uniqid() . mt_rand(10000,99999999)) . '.' . $image->extension;
            if( $image->saveAs($filePath)){
                echo json_encode([
                    'imageUrl'=>$filePath.'|;|',
                    'error'=>'',
                ]);
                exit;
            }else{
                echo json_encode([
                    'imageUrl'=>'',
                    'error'=>'保存图片失败，请重试',
                ]);
                exit;
            }
        }else{
            echo json_encode([
                'imageUrl'=>'',
                'error'=>'未获取到图片信息',
            ]);
            exit;
        }
    }

    public function actionAdd(){
        $admin = Yii::$app->user->identity->getId();
        if(empty($admin)){
            return $this->showResult(302,'用户已登出');
        }
        $photo = Yii::$app->request->post('UserPhoto')['url'];
        if(count(explode('|;|',rtrim($photo,'|;|')))>5){
            return $this->showResult(301,'最多只能存在5张相片，请先删除多余照片后再上传');
        }
        $user_id = Yii::$app->request->get('id');
        if(empty($user_id)){
            return $this->showResult(301,'读取用户信息失败');
        }
        $userPhoto = UserPhoto::findOne(['mu_id'=>$user_id]);
        if(empty($userPhoto)){
            $userPhoto = new UserPhoto();
        }
        $userPhoto->mp_other = $photo;
        $userPhoto->mu_id = $user_id;
        if(!$userPhoto->save()){
            return $this->showResult(400,'保存失败');
        }else{
            return $this->showResult(200,'保存成功');
        }
    }

    public function actionDel(){
        $mp_id = Yii::$app->request->post('mp_id');
        $url = Yii::$app->request->post('url');
        if(empty($mp_id)){
            return $this->showResult(301,'未获取到图片信息');
        }
        $admin = Yii::$app->user->identity;
        if(empty($admin)){
            return $this->showResult(302,'您的登录信息已失效，请重新登录');
        }
        $photo = UserPhoto::findOne($mp_id);
        if(empty($photo)){
            return $this->showResult(301,'该相册已不存在');
        }
        if(strpos($photo->mp_other,$url.'|;|')!==false){
            $str = str_replace($url.'|;|','',$photo->mp_other);
            $photo->mp_other = $str;
            if($photo->save()){
                return $this->showResult(200,'删除成功');
            }else{
                return $this->showResult(400,'删除失败');
            }
        }else{
            return $this->showResult(301,'未获取到图片信息');
        }
    }

}